import type { TEMP_ID_ENUM } from '@/enums';
import { useUserStore } from '@/store';
import { useMessage } from 'wot-design-uni';
import type { ToastOptions } from 'wot-design-uni/components/wd-toast/types';
/** 禁止滚动穿透方法 */
export const preventScroll = function () {
  // #ifdef MP-WEIXIN
  wx.setPageStyle({
    style: {
      overflow: 'hidden'
    }
  });
  // #endif
  // #ifdef MP-XHS
  xhs?.setBodyStyle({
    style: 'overflow: hidden;'
  });
  // #endif
  // #ifdef H5
  document.body.style.overflow = 'hidden';
  // #endif
};

/** 恢复滚动穿透方法 */
export const restoreScroll = function () {
  // #ifdef MP-WEIXIN
  wx.setPageStyle({
    style: {
      overflow: ''
    }
  });
  // #endif
  // #ifdef MP-XHS
  xhs?.setBodyStyle({
    style: ''
  });
  // #endif
  // #ifdef H5
  document.body.style.overflow = '';
  // #endif
};

export const jump = (url: string) => {
  uni.navigateTo({
    url,
    fail() {
      uni.switchTab({
        url
      });
    }
  });
};

export const isString = (value: any): boolean => {
  return Object.prototype.toString.call(value) === '[object String]';
};
export const isNumber = (value: any): boolean => {
  return Object.prototype.toString.call(value) === '[object Number]';
};

/** 检查更新 */
export const checkUpdate = () => {
  try {
    const updateManager = uni.getUpdateManager();
    updateManager.onCheckForUpdate((res) => {
      if (res.hasUpdate) {
        updateManager.onUpdateReady(() => {
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: (result) => {
              if (result.confirm) {
                updateManager.applyUpdate();
              }
            }
          });
        });
      }
    });
  } catch (e) {
    //TODO handle the exception
    console.log(e);
  }
};

/** 清除后登录重定向 */
export const clearAndRedirect = () => {
  useUserStore().clearUserInfo();
  // 登陆后跳转
  const pages = getCurrentPages();
  let redirect_url = '/pages/center/index';
  console.log('打印***pages', pages);
  if (pages.length) {
    redirect_url =
      encodeURIComponent((pages[pages.length - 1] as any)?.$page?.fullPath) ||
      '/pages/center/index';
    console.log('打印***redirect_url', redirect_url);
  }
  uni.navigateTo({
    url: `/pages/center/login/index?redirect_url=${redirect_url}`
  });
};

type TCopyOption = {
  showToast?: boolean;
  success?: () => void;
  fail?: () => void;
  complete?: () => void;
};

export const copy = (data: string, option?: TCopyOption) => {
  uni.setClipboardData({ data, ...option });
};

type TEnum = TEMP_ID_ENUM[keyof TEMP_ID_ENUM];
type TStatus = 'accept' | 'reject';
export const subscribeMsg = (id: TEnum): Promise<boolean> => {
  return new Promise((resolve) => {
    // #ifdef MP-WEIXIN
    uni.requestSubscribeMessage({
      tmplIds: [id],
      async complete(res) {
        const status: TStatus = (res as unknown as Record<string, TStatus>)[
          id as string
        ];
        resolve(status === 'accept');
      }
    });
    // #endif
  });
};
